# -*- coding: utf-8 -*-
"""
sklearn中的PCA
Created on Thu Mar 29 11:27:47 2018

@author: Allen
"""
import numpy as np
import matplotlib.pyplot as plt

X = np.empty( ( 100, 2 ) )
X[:,0] = np.random.uniform( 0., 100., size = 100 )
X[:,1] = 0.75 * X[:,0] + 3. + np.random.normal( 0, 10, size=100 )

from sklearn.decomposition import PCA

pca = PCA( n_components = 1 )
pca.fit( X )
X_reduction = pca.transform( X )
X_restore = pca.inverse_transform( X_reduction )

plt.scatter( X[:, 0], X[:, 1], color = "b", alpha = 0.5 )
plt.scatter( X_restore[:, 0], X_restore[:, 1], color = "r", alpha = 0.5 )
plt.show()